package com.example.constellation.mefrag;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.constellation.R;
import com.example.constellation.bean.StarInfoBean;
import com.example.constellation.luckfrag.LuckBaseAdapter;
import com.example.constellation.util.AssetsUtil;
import de.hdodenhof.circleimageview.CircleImageView;

import java.util.List;
import java.util.Map;


/**
 * 我的页面
 */
public class MeFragment extends Fragment {
    CircleImageView iconIv;
    TextView nameTv;
    private Map<String, Bitmap> imgmap;
    private List<StarInfoBean.StarinfoBean> mDatas;
    //保存选择的星座(共享参数)
    private SharedPreferences star_name;
    //保存选择星座的位置
    int selpos=0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取activity传递的数据
        Bundle bundle=getArguments();
        StarInfoBean infoBean=(StarInfoBean)bundle.getSerializable("info");
        mDatas=infoBean.getStarinfo();
        star_name=getContext().getSharedPreferences("star_name", Context.MODE_PRIVATE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_me, container, false);
        iconIv=view.findViewById(R.id.mefrag_iv);
        nameTv=view.findViewById(R.id.mefrag_tv_name);
        //进行初始化设置
        imgmap= AssetsUtil.getContentlogoImgMap();
        //从共享参数中读取
        String name=star_name.getString("name","巨蟹座");
        String logoname=star_name.getString("logoname","jvxie");
        nameTv.setText(name);
        Bitmap bitmap=imgmap.get(logoname);
        iconIv.setImageBitmap(bitmap);
        iconIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }

            private void showDialog() {
                final Dialog dialog=new Dialog(getContext());
                //弹出页面为me_dialog.xml
                View dialogview=LayoutInflater.from(getContext()).inflate(R.layout.me_dialog,null);
                dialog.setContentView(dialogview);
                dialog.setTitle("请选择您的星座");
                GridView gridView=dialogview.findViewById(R.id.mefrag_dialog_gv);
                //设置适配器
                LuckBaseAdapter adapter=new LuckBaseAdapter(getContext(),mDatas);
                gridView.setAdapter(adapter);
                //设置能否被取消
                dialog.setCancelable(true);
                //设置点击弹出框其余部分,即可取消
                dialog.setCanceledOnTouchOutside(true);

                //设置gridview每一项的点击事件
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        StarInfoBean.StarinfoBean bean=mDatas.get(i);
                        String name=bean.getName();
                        String logoname=bean.getLogoname();
                        nameTv.setText(name);
                        Bitmap bitmap=imgmap.get(logoname);
                        iconIv.setImageBitmap(bitmap);
                        selpos=i;//保存选择的位置
                        dialog.cancel();
                    }
                });
                dialog.show();
            }
        });
        View about1=view.findViewById(R.id.mefrag_tv_about1);
        View about2=view.findViewById(R.id.mefrag_tv_about2);
        View about3=view.findViewById(R.id.mefrag_tv_about3);
        View about4=view.findViewById(R.id.mefrag_tv_about4);
        about1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    showExitDialog("'关于App'功能未开发...");
            }
            private void showExitDialog(String msg){
                new AlertDialog.Builder(getContext())
                        .setTitle("开发提示")
                        .setMessage(msg)
                        .setPositiveButton("好滴吧", null)
                        .show();
            }
        });
        about2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showExitDialog("'给予评价'功能未开发...");
            }
            private void showExitDialog(String msg){
                new AlertDialog.Builder(getContext())
                        .setTitle("开发提示")
                        .setMessage(msg)
                        .setPositiveButton("好滴吧", null)
                        .show();
            }
        });
        about3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showExitDialog("'反馈问题'功能未开发...");
            }
            private void showExitDialog(String msg){
                new AlertDialog.Builder(getContext())
                        .setTitle("开发提示")
                        .setMessage(msg)
                        .setPositiveButton("好滴吧", null)
                        .show();
            }
        });
        about4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showExitDialog("'人工客服'功能未开发...");
            }
            private void showExitDialog(String msg){
                new AlertDialog.Builder(getContext())
                        .setTitle("开发提示")
                        .setMessage(msg)
                        .setPositiveButton("好滴吧", null)
                        .show();
            }
        });
        return view;

    }

    @Override
    public void onPause() {
        super.onPause();
        StarInfoBean.StarinfoBean bean=mDatas.get(selpos);
        String name=bean.getName();
        String logoname=bean.getLogoname();
        SharedPreferences.Editor editor=star_name.edit();//获取向共享参数中写入数据的对象;
        editor.putString("name",name);
        editor.putString("logoname",logoname);
        editor.commit();//提交
    }




}
